<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单金额" prop="orderPrice">
        <el-input
          v-model="queryParams.orderPrice"
          placeholder="请输入订单金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货商品" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入退货商品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="用户手机号" prop="mbnum">
        <el-input
          v-model="queryParams.mbnum"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="退货原因" prop="refundMsg">
        <el-input
          v-model="queryParams.refundMsg"
          placeholder="请输入退货原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款结果" prop="returnMsg">
        <el-input
          v-model="queryParams.returnMsg"
          placeholder="请输入退款结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款时间" prop="returnTime">
        <el-date-picker
          clearable
          v-model="queryParams.returnTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择退款时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:refund:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:refund:edit']"
          >修改</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:refund:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:refund:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="refundList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderSn" />
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="退货商品" align="center" prop="goodsName" />
      <el-table-column label="用户手机号" align="center" prop="mbnum" />
      <el-table-column label="退货原因" align="center" prop="refundMsg" />
      <el-table-column label="退款状态" align="center" prop="isStatus">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.isStatus == 1">待处理</el-tag>
          <el-tag type="success" v-if="scope.row.isStatus == 2">已退款</el-tag>
          <el-tag type="danger" v-if="scope.row.isStatus == 3">拒绝退款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="退款结果" align="center" prop="returnMsg" />
      <el-table-column
        label="退款时间"
        align="center"
        prop="returnTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdacl(scope.row)"
            v-if="scope.row.isStatus == 1"
            v-hasPermi="['order:refund:returnAfterPrice']"
            >处理</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:refund:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后退款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单金额" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="退货商品" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入退货商品" />
        </el-form-item>
        <el-form-item label="用户手机号" prop="mbnum">
          <el-input v-model="form.mbnum" placeholder="请输入用户手机号" />
        </el-form-item>
        <el-form-item label="退货原因" prop="refundMsg">
          <el-input v-model="form.refundMsg" placeholder="请输入退货原因" />
        </el-form-item>
        <el-form-item label="退款结果" prop="returnMsg">
          <el-input v-model="form.returnMsg" placeholder="请输入退款结果" />
        </el-form-item>
        <el-form-item label="退款时间" prop="returnTime">
          <el-date-picker
            clearable
            v-model="form.returnTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退款时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 退款 -->
    <el-dialog
      title="退款"
      :visible.sync="openDelis2"
      width="700px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="formOne2"
        :rules="rules"
        label-width="110px"
        size="mini"
      >
        <el-form-item label="退款商品:" label-width="220">{{
          formOne2.goodsName
        }}</el-form-item>

        <el-form-item label="订单金额：" label-width="220">{{
          formOne2.orderPrice
        }}</el-form-item>

        <el-form-item label="用户手机号：" label-width="220">
          <!-- shippingAddress -->
          {{ formOne2.mbnum }}
        </el-form-item>
        <el-form-item label="退款时间：" label-width="220">
          <!-- shippingAddress -->
          {{ formOne2.returnTime }}
        </el-form-item>
        <!-- <el-col :span="26"> -->
        <el-form-item label="退款原因：" label-width="220">
          {{ formOne2.refundMsg }}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="qd2">退 款</el-button>
        <el-button @click="qx2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRefund,
  getRefund,
  delRefund,
  addRefund,
  updateRefund,
  returnAforder,
  returnAfterPrice,
} from "@/api/system/refund";

export default {
  name: "Refund",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后退款表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSn: null,
        userId: null,
        orderPrice: null,
        goodsName: null,
        mbnum: null,
        refundMsg: null,
        isStatus: null,
        returnMsg: null,
        returnTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //
      openDelis2: false,
      formOne2: {},
      //
      id: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询售后退款列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then((response) => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderSn: null,
        userId: null,
        orderPrice: null,
        goodsName: null,
        mbnum: null,
        refundMsg: null,
        createTime: null,
        isStatus: null,
        returnMsg: null,
        returnTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加售后退款";
    },
    // 处理
    handleUpdacl(v) {
      this.openDelis2 = true;
      this.formOne2 = v;
      this.id = v.id;
      // console.log(v.id);
    },
    // 退款
    qd2() {
      let v = {
        id: this.id,
      };
      returnAfterPrice(v).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.$modal.msgSuccess(res.msg);
          this.openDelis2 = false;
          this.getList();
        }
      });
    },
    // 取消
    qx2() {
      this.openDelis2 = false;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRefund(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改售后退款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRefund(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefund(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除售后退款编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRefund(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/refund/export",
        {
          ...this.queryParams,
        },
        `refund_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
